<?php
	/**
	 * 格式化打印数组
	 */
	function p ($arr){
		echo '<pre>';
		print_r($arr);
		echo '</pre>';
	}

	/**
	 * 递归无限级分类
	 * @param  [type]  $arr   要处理的数组
	 * @param  integer $pid   父类id
	 * @param  integer $type  处理类型
	 * @param  integer $level 所属级别
	 * @return [array]        处理好的数组
	 */
	function recursion($arr , $pid=0 , $type=1 , $level=0){

		switch($type){
			case 1: //递归返回所有无限级栏目
				static $array1 = array();
				static $id1 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array1[$id1] = $val;
						$array1[$id1]['html'] = str_repeat('--', $level);
						$id1++;
						recursion($arr , $val['id'] , $type ,$level+1);
					}
				}
				return $array1;
				break;
			case 2: //传递分类id返回所有父级
				static $array2 = array();				
				foreach($arr as $val){
					if($val['id'] == $pid){
						$array2[$val['id']] = $val;
						recursion($arr , $val['pid'] , $type);
					}
				}
				return array_reverse($array2);
				break;
			case 3: //递归返回所有无限级栏目,并标记顶级分类
				static $array3 = array();
				static $id3 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						if($pid == 0){
							$tmpVal = $val['name'];
							$val['name'] = '<font size="2"><b>'.$tmpVal.'</b></font>';
						}
						$array3[$id3] = $val;
						$array3[$id3]['html'] = str_repeat('--', $level);
						$id3++;
						recursion($arr , $val['id'] , $type ,$level+1);
					}
				}
				return $array3;
				break;
			case 4://传递一个id返回所有子类的id
				static $array4 = array();
				static $id4 = 0;
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array4[$id4] = $val;
						$array4[$id4]['html'] = str_repeat('--', $level);
						$id4++;
						recursion($arr , $val['id'] , $type , $level+1);
					}
				}
				return $array4;
				break;
			case 5: //按父子关系等级返回多维数组
				$array = array();
				foreach($arr as $val){
					if($val['pid'] == $pid){
						$array[$val['id']] =$val;
						$array[$val['id']]['children'] = recursion($arr , $val['id'] , $type);
					}
				}
				return $array;
				break;
			default :
				return false;
		}
		
	}
	/**
	 * [buyCartStatistics 统计购物车中所有商品的规格属性及数量]
	 * @param  [type] $where [请求来自何处]
	 * @return [type]        [description]
	 */
	function buyCartStatistics($where){
		// $_SESSION['cart']=null;die;
		$cartGsNum = count($_SESSION['cart']);
		// echo 'cgsnum'.$cartGsNum.'|';
		// p($_SESSION['cart']);
		$cartUseGsNum = 0;
		for($i=0; $i < $cartGsNum; $i++){
			for ($j=$i+1; $j < $cartGsNum; $j++) { 
				$result = array_diff_assoc($_SESSION['cart'][$i], $_SESSION['cart'][$j]);
				// p($result);
				$condition = ($result['number'] > 0 ? 1 : 0) && (count($result)==1 || (isset($result['totalPrice']) && count($result)==2));
				// echo '--'.$i.'--';
				// echo $condition;
				// echo '--'.$j.'--';
				if(count($result) == 0 || $condition){
					if($where=='gscpage'){
						$_SESSION['cart'][$i]['number'] = (int)$_SESSION['cart'][$i]['number'] + (int)$_SESSION['cart'][$j]['number'];
						unset($_SESSION['cart'][$j]);
						$cartGsNum--;
					}else if($where=='cart'){
						$_SESSION['cart'][$i]['number'] = (int)$_SESSION['cart'][$j]['number'];
						$_SESSION['cart'][$i]['totalPrice'] =  $_SESSION['cart'][$j]['totalPrice'];
						unset($_SESSION['cart'][$j]);
						$cartGsNum--;
					}
				}
			}
		}
		// p($_SESSION['cart']);
		// die;
		foreach($_SESSION['cart'] as $val){
			$cartUseGsNum += (int)$val['number'];
		}
		$_SESSION['cartGsNum'] = $cartUseGsNum;
	}
?>